// pages/checkIn/imgPreview/imgPreview.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        realName: '',
        currentImgs: [], //图片数组
        current: Number,
        descriptionArr: [], //图片描述
        realNameArr: [], //发布真实姓名
        timeArr: [], //图片发布时间
        idArr: [], //图片id
        userIdArr: [], //创建人Uid
        hideBottom: false,
        show: false,
        actions: [
            {
                name: '删除',
                color: '#FF455E',
            },
        ],
        refresh: false,
        fromWhere: '', //00我的页面照片墙 01秘书团页面 02同事页面照片墙
        activeCreateUid: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.fromWhere == '01') {
            wx.setNavigationBarTitle({
                title: app.globalData.addressBack + '·图片',
            });
        } else {
            wx.setNavigationBarTitle({
                title: '图片预览',
            });
        }
        let pages = getCurrentPages(); //当前页面栈
        let prevPage = pages[pages.length - 2]; //上一页面
        this.setData({
            realName: app.globalData.realname,
            realNameArr: prevPage.data.userArr,
            currentImgs: prevPage.data.sceneImageArr,
            current: parseInt(options.current || prevPage.data.currentPicIndex),
            descriptionArr: prevPage.data.description,
            timeArr: prevPage.data.timeArr,
            idArr: prevPage.data.idArr,
            userIdArr: prevPage.data.userIdArr,
            hideBottom: options.hideBottom,
            fromWhere: options.fromWhere,
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    onUnload() {
        if (this.data.refresh) {
            let pages = getCurrentPages(); //当前页面栈
            let prevPage = pages[pages.length - 2]; //上一页面
            if (typeof prevPage.refresh == 'function') {
                prevPage.refresh();
            }
        }
    },
    // 预览图片
    previewImage(e) {
        let index = e.currentTarget.dataset.index;
        wx.previewImage({
            current: this.data.currentImgs[index],
            urls: this.data.currentImgs,
        });
    },

    change(e) {
        if (e.detail.source === 'touch') {
            this.setData({
                current: e.detail.current,
            });
        }
    },
    // show删除弹框
    showcover(e) {
        if (!this.data.hideBottom) {
            this.setData({
                show: true,
                activeCreateUid: e.currentTarget.dataset.uid,
            });
        }
    },
    //取消
    onCancel(e) {
        this.setData({
            show: false,
        });
    },
    onDelect(e) {
        let _that = this;
        //是否有权限
        if (
            app.globalData.authority == '00' ||
            app.globalData.authority == '01' ||
            (app.globalData.authority == '04' &&
                _that.data.fromWhere != '02') ||
            (app.globalData.authority == '05' &&
                _that.data.fromWhere != '02') ||
            _that.data.activeCreateUid == app.globalData.uid ||
            _that.data.fromWhere == '00'
        ) {
            let current = e.currentTarget.dataset.current;
            wx.request({
                url: app.globalData.rootPath + `cjMap/deletePhotoWallById`,
                method: 'GET',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                },
                data: {
                    authId: app.globalData.authId,
                    id: _that.data.idArr[e.currentTarget.dataset.current],
                },
                success: function (res) {
                    let realName = _that.data.realNameArr;
                    let currentImg = _that.data.currentImgs;
                    let description = _that.data.descriptionArr;
                    let id = _that.data.idArr;
                    let uid = _that.data.userIdArr;
                    let time = _that.data.timeArr;
                    realName.splice(current, 1);
                    currentImg.splice(current, 1);
                    description.splice(current, 1);
                    id.splice(current, 1);
                    uid.splice(current, 1);
                    time.splice(current, 1);
                    //删除全部照片后返回上一页
                    if (realName.length == 0) {
                        wx.navigateBack({
                            detal: 1,
                        });
                    }
                    _that.setData({
                        realNameArr: realName,
                        currentImgs: currentImg,
                        descriptionArr: description,
                        idArr: id,
                        userIdArr: uid,
                        timeArr: time,
                        show: false,
                        refresh: true,
                        current:
                            _that.data.current == 0
                                ? _that.data.current
                                : _that.data.current - 1,
                    });
                    wx.showToast({
                        title: '已删除',
                        icon: 'none',
                        duration: 1500,
                    });
                },
            });
        } else {
            wx.showToast({
                title: '暂无权限',
                icon: 'none',
                duration: 1500,
            });
            _that.setData({
                show: false,
            });
        }
    },
});
